package com.addict.applocker.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.addict.applocker.R;
import com.addict.applocker.manager.SettingManager;

import java.util.ArrayList;

public class SelectThemeActivity extends AppCompatActivity implements View.OnClickListener {
    private ArrayList<Integer> mDefineColor = new ArrayList<>();

    private Toolbar mcToolBar = null;
    private ImageView mcPreview = null;
    private LinearLayout mcColorPanel = null;

    private void initColorArray() {
        // 怀念C艹的#define......
        mDefineColor.add(getResources().getColor(R.color.color_theme_1));
        mDefineColor.add(getResources().getColor(R.color.color_theme_2));
        mDefineColor.add(getResources().getColor(R.color.color_theme_3));
        mDefineColor.add(getResources().getColor(R.color.color_theme_4));
        mDefineColor.add(getResources().getColor(R.color.color_theme_5));
        mDefineColor.add(getResources().getColor(R.color.color_theme_6));
        mDefineColor.add(getResources().getColor(R.color.color_theme_7));
        mDefineColor.add(getResources().getColor(R.color.color_theme_8));
        mDefineColor.add(getResources().getColor(R.color.color_theme_9));
        mDefineColor.add(getResources().getColor(R.color.color_theme_10));
        mDefineColor.add(getResources().getColor(R.color.color_theme_11));
        mDefineColor.add(getResources().getColor(R.color.color_theme_12));
        mDefineColor.add(getResources().getColor(R.color.color_theme_13));
        mDefineColor.add(getResources().getColor(R.color.color_theme_14));
        mDefineColor.add(getResources().getColor(R.color.color_theme_15));
        mDefineColor.add(getResources().getColor(R.color.color_theme_16));
        mDefineColor.add(getResources().getColor(R.color.color_theme_17));
        mDefineColor.add(getResources().getColor(R.color.color_theme_18));
        mDefineColor.add(getResources().getColor(R.color.color_theme_19));
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_theme);
        initColorArray();

        mcToolBar = (Toolbar) findViewById(R.id.toolbar);
        mcPreview = (ImageView) findViewById(R.id.select_preview);
        mcColorPanel = (LinearLayout) findViewById(R.id.select_panel);

        mcPreview.setBackgroundColor(SettingManager.getInstance().getThemeColor());
        mcToolBar.setBackgroundColor(SettingManager.getInstance().getThemeColor());
        mcToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        for(int i = 0; i < mDefineColor.size(); i++) {
            View v = new View(this);
            v.setBackgroundColor(mDefineColor.get(i));
            v.setOnClickListener(this);

            int viewSize = (int) dpToPx(this, 70); //设置为70dp
            int margin = (int) dpToPx(this, 10);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(viewSize, viewSize);
            params.setMargins(margin, margin, margin, margin);

            mcColorPanel.addView(v, params);
        }
    }

    public static float dpToPx(Context ctx, float dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, ctx.getResources().getDisplayMetrics());
    }

    @Override
    public void onClick(View v) {
        for (int i = 0; i < mcColorPanel.getChildCount(); i++) {
            if (v == mcColorPanel.getChildAt(i)) {
                mcToolBar.setBackgroundColor(mDefineColor.get(i));
                mcPreview.setBackgroundColor(mDefineColor.get(i));
                SettingManager.getInstance().setThemeColor(mDefineColor.get(i));
                break;
            }
        }
    }
}
